/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.namabank.helpdesk.model;

import com.namabank.db.GlobalHibernate;
import com.namabank.helpdesk.dbobj.SMTB_CURRENT_USER;
import com.namabank.helpdesk.dbobj.SMTB_USER_LOGIN_LOG;
import com.namabank.util.ClientInfo;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Nguyen Tran Hien
 */
public class CurrentUserModel {
    public CurrentUserModel(){
        dbObj = new SMTB_CURRENT_USER();
        dbObj.setLOGIN_STAMP(new Date());
    }
    public CurrentUserModel(SMTB_CURRENT_USER instance){
        dbObj = instance;
    }

    private SMTB_CURRENT_USER dbObj;
    
    // <editor-fold defaultstate="collapsed" desc="Những hàm public">
    public void setClientInfo(ClientInfo info){
        if(dbObj == null)
            return;
        if(info == null)
            info = new ClientInfo();
        dbObj.setCLIENT_BROWSER_NAME(info.getBrowserName());
        dbObj.setCLIENT_BROWSER_VERSION(info.getBrowserVersion());
        dbObj.setCLIENT_IP(info.getIP());
        dbObj.setCLIENT_OS(info.getOS());
    }
    public void setUserInfo(UserModel md){
        if(dbObj != null && md != null && md.getSmtbUserObj() != null){
            dbObj.setUSER_ID(md.getSmtbUserObj().getUSER_ID());
            dbObj.setUSER_NAME(md.getSmtbUserObj().getUSER_NAME());
        }
    }
    public void setSessionId(String id){
        if(dbObj == null)
            return;
        dbObj.setLOGIN_SESSION(id);
    }
    public void setLoginStamp(Date dt){
        if(dbObj == null)
            return;
        dbObj.setLOGIN_STAMP(dt);
    }
    public SMTB_USER_LOGIN_LOG genLogObj(){
        if(dbObj == null){
            return null;
        }
        SMTB_USER_LOGIN_LOG res = new SMTB_USER_LOGIN_LOG();
        res.setCLIENT_BROWSER_NAME(dbObj.getCLIENT_BROWSER_NAME());
        res.setCLIENT_BROWSER_VERSION(dbObj.getCLIENT_BROWSER_VERSION());
        res.setCLIENT_IP(dbObj.getCLIENT_IP());
        res.setCLIENT_OS(dbObj.getCLIENT_OS());
        res.setLOGIN_SESSION(dbObj.getLOGIN_SESSION());
        res.setLOGIN_STAMP(dbObj.getLOGIN_STAMP());
        res.setUSER_AGENT(dbObj.getUSER_AGENT());
        res.setUSER_ID(dbObj.getUSER_ID());
        res.setUSER_NAME(dbObj.getUSER_NAME());
        
        return res;
    }

    /**
     * @return the instance
     */
    public SMTB_CURRENT_USER getDbObj() {
        return dbObj;
    }

    public String getUserId(){
        return dbObj == null?null:dbObj.getUSER_ID();
    }
    public String getUserName(){
        return dbObj == null?null:dbObj.getUSER_NAME();
    }
    public String getSessionId(){
        return dbObj == null?null:dbObj.getLOGIN_SESSION();
    }
    // </editor-fold>


    // <editor-fold defaultstate="collapsed" desc="Những hàm static">
    public static List<SMTB_CURRENT_USER> getSMTB_CURRENT_USER_by_UserId(String userId){
        String qry = "from SMTB_CURRENT_USER where USER_ID = '" + userId + "'";
        return (List<SMTB_CURRENT_USER>) GlobalHibernate.openSession().createQuery(qry).list();
    }
    public static CurrentUserModel getBySessionId(String sessionId){
        String qry = "from SMTB_CURRENT_USER where LOGIN_SESSION = '" + sessionId + "'";
        SMTB_CURRENT_USER obj = (SMTB_CURRENT_USER) GlobalHibernate.openSession().createQuery(qry).uniqueResult();
        if(obj == null)
            return null;
        return new CurrentUserModel(obj);
    }
    // </editor-fold>
}
